<?php
namespace app\shop\controller;
use think\Controller;
use think\Db;
use lib\UserCrypt;
class Login extends Controller{
    public function login(){
        if(request()->isGet()){
            $url=$this->request->domain();
            $this->assign('url',$url);
            return $this->fetch();
        }elseif(request()->isPost()){
            $phone = input("param.phone");
            $password = input("param.password");
            $hasUser = Db::name('users')->where(['phone'=> $phone])->find();
            if(empty($hasUser)){
                return json(['code' => 501, 'msg' => '用户不存在']);
            }elseif(md5(md5('tmd'.$password))!=$hasUser['password']){
                return json(['code' => 502,'msg' => '密码错误']);
            }else{
                $res['Token']=UserCrypt::do_mencrypt($hasUser['id']);
                session('userinfo', $hasUser);
                return json(['code' => 200, 'msg' => '成功登录','data'=>$res]);
            }
        }
        
    }
    public function register(){
        if(request()->isGet()){
            $url=$this->request->domain();
            $this->assign('url',$url);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['username']=$param['username'];
            $data['phone']=$param['phone'];
            if($param['confirm_password']!=$param['password']){
                return json(['code' => 501, 'msg' => '密码与确认密码不一样']);
            }
            $data['password']=md5(md5('tmd'.input("param.password")));
            if(!empty(input("param.invite_code"))){
                $data['invite_code']=input("param.invite_code");
                $invite_code=Db::name('users')->where(array('invite_code'=>$data['invite_code']))->find();
                if($invite_code){
                }else{
                    return json(['code' => 502, 'msg' => '邀请码错误']);
                }
            }
            $username=Db::name('users')->where(array('username'=>$data['username']))->find();
            if($username){
                return json(['code' => 503, 'msg' => '用户名已存在']);
            }
            $user=Db::name('users')->where(array('phone'=>$data['phone']))->find();
            if ($user) {
                return json(['code' => 504, 'msg' => '此账号已注册']);
            }else{
                if(isset($invite_code)){
                    $data['invite_id']=$invite_code['id'];
                }
                $data['invite_code']=$this->inviteCode();
            }
            $data['time']=time();
            $data['uptime']=time();
            $id=Db::name('users')->insertGetId($data);
            if($id){
                $res['Token']=UserCrypt::do_mencrypt($id);
                session('userinfo', $username);
                return json(['code' => 200, 'msg' => '注册成功','data'=>$res]);
            }else{
                return json(['code' => 500, 'msg' => '注册失败']);
            }
            
        }
    }
    public function login_out(){
    	session('userinfo',null);
    	return json(['code' => 200, 'msg' => '成功']);
    }
    private function inviteCode()
    {
        $invite_code=$this->inviteCodeRandom(8);
        $isInviteCode=Db::name('users')->where(array('invite_code'=>$invite_code))->find();
        if($isInviteCode){
            $this->inviteCode();
        }else{
            return $invite_code;
        }
    }
    private function inviteCodeRandom($length = 8) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
          $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}
